package com.akeil.flash.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.akeil.flash.dialog.QueryConditionDialog;
import com.akeil.flash.pojo.Data;
import com.akeil.flash.service.IDataService;
import com.akeil.flash.view.HistoryDataView;

import flash.Activator;
import flash.Application;

public class QueryConditionAction extends AbstractFlashAction {
	IDataService dataService = (IDataService)Application.applicationContext.getBean("dataServiceImpl");
	private ViewPart viewPart;
	List<Data> datas = new ArrayList<Data>();
	
	public QueryConditionAction(ViewPart viewPart){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/history/search_16px.png"));
		this.setText(resourceBundle.getString("toolbar.query.condition.name"));
		this.setToolTipText(resourceBundle.getString("toolbar.query.condition.tooltip"));
		this.viewPart = viewPart;
	}
	
	@Override
	public void run() {
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		QueryConditionDialog dialog = new QueryConditionDialog(parentShell);
		if(IDialogConstants.OK_ID == dialog.open()){
			String startDate = dialog.getStart();
			String endDate = dialog.getEnd();
			String sampleName = dialog.getSampleName();
			String sampleNo = dialog.getSampleNo();
			
			datas = dataService.selectDataByPeriodAndName(startDate, endDate, sampleName,sampleNo);
			
			((HistoryDataView)viewPart).refreshData(datas);
		}
		
		super.run();
	}

	public List<Data> getDatas() {
		return datas;
	}
}
